import { defineStore } from 'pinia'
import { MenuThemeType } from '../../types/store'
import { ThemeList } from '../../config/setting'
import { GlopThemeEnum, MenuThemeEnum } from '../../enums/appEnums'

export interface SettingState {
  glopThemeType: GlopThemeEnum    // 全局主题类型
  menuThemeType: MenuThemeEnum    // 菜单主题类型
  uniqueOpened: boolean           // 是否开启手风琴模式
  showMenuButton: boolean         // 是否显示菜单展开按钮
  showRefreshButton: boolean      // 是否显示页面刷新按钮
  showCrumbs: boolean             // 是否显示全局面包屑
  autoClose: boolean              // 设置后是否自动关闭窗口
  showWorkTab: boolean            // 是否显示多标签
  showLanguage: boolean           // 是否显示多语言选择
  showNprogress: boolean          // 是否显示顶部进度条
  refresh: boolean
} 

export const useSettingStore = defineStore({
  id: 'settingStore',
  state: (): SettingState => ({
    glopThemeType: GlopThemeEnum.LIGHT,
    menuThemeType: MenuThemeEnum.DESIGN,
    uniqueOpened: true,
    showMenuButton: true,
    showRefreshButton: true,
    showCrumbs: true,
    autoClose: false,
    showWorkTab: true,
    showLanguage: true,
    showNprogress: false,
    refresh: false
  }),
  getters: {
    getMenuTheme(): MenuThemeType {
      let list = ThemeList.filter((item) => item.theme === this.menuThemeType)
      return list[0]
    }
  },
  actions: {
    // 初始化state
    initState() {
      let sys: any = localStorage.getItem("sys")

      if(sys) {
        sys = JSON.parse(sys)
        let { setting } = sys.user
        this.glopThemeType = setting.glopThemeType
        this.menuThemeType = setting.menuThemeType
        this.uniqueOpened = setting.uniqueOpened
        this.showMenuButton = setting.showMenuButton
        this.showRefreshButton = setting.showRefreshButton
        this.showCrumbs = setting.showCrumbs
        this.autoClose = setting.autoClose
        this.showWorkTab = setting.showWorkTab
        this.showLanguage = setting.showLanguage
        this.showNprogress = setting.showNprogress
      }
    },
    setGlopTheme(theme: GlopThemeEnum) {
      this.glopThemeType = theme
    },
    setMenuTheme(theme: MenuThemeEnum) {
      this.menuThemeType = theme
    },
    // 设置菜单是否为手风琴模式
    setUniqueOpened() {
      this.uniqueOpened = !this.uniqueOpened
    },
    // 显示侧边栏折叠按钮
    setButton() {
      this.showMenuButton = !this.showMenuButton
    },
    // 是否自动关闭个性化设置
    setAutoClose() {
      this.autoClose = !this.autoClose
    },
    // 是否显示页面刷新按钮
    setShowRefreshButton() {
      console.log(this.showMenuButton)
      this.showRefreshButton = !this.showRefreshButton
    },
    // 是否显示面包屑导航
    setCrumbs() {
      this.showCrumbs = !this.showCrumbs
    },
    // 是否显示多标签
    setWorkTab(show: boolean) {
      this.showWorkTab = show
    },
    // 是否显示多语言选择
    setLanguage() {
      this.showLanguage = !this.showLanguage
    },
    // 是否显示顶部进度条
    setNprogress() {
      this.showNprogress = !this.showNprogress
    },
    // 刷新当前页
    reload() {
      this.refresh = !this.refresh
    }
  }
})